import { Request, Response } from 'express'
import { followService } from '../services/follow'

export const followUser = async (req: Request, res: Response) => {
  try {
    const { targetId } = req.params
    const result = await followService.followUser(req.user!.id, targetId)
    res.json(result)
  } catch (error) {
    res.status(400).json({ message: error.message })
  }
}

export const unfollowUser = async (req: Request, res: Response) => {
  try {
    const { targetId } = req.params
    const result = await followService.unfollowUser(req.user!.id, targetId)
    res.json(result)
  } catch (error) {
    res.status(400).json({ message: error.message })
  }
}

export const getFollowers = async (req: Request, res: Response) => {
  try {
    const { userId } = req.params
    const { page, limit } = req.query
    const result = await followService.getFollowers(userId, Number(page), Number(limit))
    res.json(result)
  } catch (error) {
    res.status(500).json({ message: '服务器错误' })
  }
}

export const getFollowing = async (req: Request, res: Response) => {
  try {
    const { userId } = req.params
    const { page, limit } = req.query
    const result = await followService.getFollowing(userId, Number(page), Number(limit))
    res.json(result)
  } catch (error) {
    res.status(500).json({ message: '服务器错误' })
  }
}
